<template>
  <div>
    <Searchbar>
      <el-input @change="getList" v-model="search.course_id" placeholder="请输入课程ID"></el-input>
      <el-input @change="getList" v-model="search.course_name" placeholder="请输入已开课程"></el-input>
      <el-input @change="getList" v-model="search.teacher_name" placeholder="请输入老师名称"></el-input>

      <el-select
        @change="onCourse_typeChange"
        v-model="search.course_type"
        placeholder="请选择类型"
      >
        <el-option
          v-for="item in course_types"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-select
        @change="onCourse_statusChange"
        v-model="search.course_status"
        placeholder="请选择状态"
      >
        <el-option
          v-for="item in course_status"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
    </Searchbar>
    <TableBar>
      <el-button @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>
      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
      <el-button @click="onMulForbidden" icon="el-icon-lock">批量下架</el-button>
      <el-button @click="onMulEnable" icon="el-icon-unlock">批量上架</el-button>
    </TableBar>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
      <el-table-column prop="course_id" label="课程ID" ></el-table-column>
      <el-table-column prop="course_name" label="课程名称" ></el-table-column>
      <el-table-column prop="teachers" label="任课老师" ></el-table-column>
      <el-table-column prop="courseTypeLabel" label="课程类型" ></el-table-column>
      <el-table-column prop="capacity" label="选课容量" ></el-table-column>
      <el-table-column prop="choice_code" label="选课码" ></el-table-column>
      <el-table-column prop="student_count" label="已选人数" ></el-table-column>
      <el-table-column prop="statusLabel" label="课程状态" ></el-table-column>
      <el-table-column prop="create_time" label="创建时间" ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="onShowDetail(scope.row)"
            size="small"
            class="text-button"
          >查看</el-button>
          <el-button @click="onShowEdit(scope.row)" size="small" class="text-default">编辑</el-button>
          <el-button
            @click="onForbidden(scope.row)"
            size="small"
            :class="scope.row.status == 1 ? 'text-warning' : 'text-button'"
          >{{scope.row.status == 1 ? '下架' : '上架'}}</el-button>
          <el-button @click="onDelete(scope.row)" size="small" class="text-warning">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <EditCourse @onClose="onHideEdit" @afterSubmit="getList" :obj="editRow" :show="showEdit" />

    <MulUpload :temp="8" :type="3" :action="'/object/batch/create/'" @onClose="onHideUpload" :show="showUpload" />
  </div>
</template>
<script>
import EditCourse from "@/modals/EditCourse";
import MulUpload from "@/components/MulUpload";
import CreateFilter from "@/mixins/CreateFilter";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import blockMixin from "@/mixins/blockMixin";
import { courseModel } from "@/api/admin_models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import {Message} from 'element-ui';
export default {
  mixins: [CreateFilter, showEditMixin, uploadMixin, pageMixin, blockMixin],
  components: { EditCourse, MulUpload, Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        course_id: null,
        course_name: null,
        teacher_name: null,
        course_type: null,
        course_status: null,
        page: 1,
        page_size: 10
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    batchBlockFun() {
      return courseModel.batchBlock;
    },
    onShowDetail(row) {
      this.$router.push('/course/' + row.course_id)
      console.log(row);
    },
    onDelete(row) {
      this.$confirm(`确定删除${row.course_name}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        }).catch((err) => {
          this.$message({
            type: 'info',
            message: err.message
          });          
        });
    },
    getList() {
      console.log("getList");
      courseModel.list(
        this.search,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            v.courseTypeLabel = this.courseTypeToLabel(v.course_type);
            v.statusLabel = this.courseStatusToLabel(v.status);
            v.teachers = v.teacher_set.map((v) => {
              return v.name
            }).join(',');
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    },
    doForbidden(ids, status, msg) {
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.batchBlockFun()({
          courses: ids,
          status: status
        }, res => {
          this.$message({
            type: 'success',
            message: res.message
          });
          this.getList();
        }, err => { console.log(err) })
      }).catch((err) => {
        console.error(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    onMulForbidden() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要处理课程');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.course_id;
      })
      this.doForbidden(ids, 2, '确定批量下架课程？');
    },
    onMulEnable() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要处理课程');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.course_id;
      })
      this.doForbidden(ids, 1, '确定批量上架课程？');
    },
    onForbidden(row) {
      debugger
      let ope = row.status == 1 ? '下架' : '上架';
      let status = row.status == 1 ? 2 : 1;
      this.doForbidden([row.course_id], status, `确定${ope}课程${row.course_name}?`);
    }
  }
};
</script>
<style lang="scss">
</style>